
//注销登录
function logout() {
    if(confirm("确定注销?")) {
        jQuery.ajax({
            type: 'POST',
            url: '/user/logout',
            data: {},
            success: function(result) {
                if(result != null && result.code == 200) {
                    //注销成功，重定向到主页
                    location.assign("login.html");
                } else {
                    alert("很抱歉！注销失败，请稍后重试!");
                }
            }
        });
    }
}

//获取当前 url 参数的公共方法
function getUrlValue(key) {
    var params = location.search;
    if(params.length > 1) {
        //ex:?id=1&v=2
        params = params.substring(1);//去掉问号
        var paramArr = params.split("&");
        for(var i = 0; i < paramArr.length; i++) {
            var kv = paramArr[i].split("=");
            if(kv[0] == key) {
                //是我要查询的参数
                return kv[1];
            }
        }
    }
    //未查询到结果
    return "";
}


//登录 / 注销
function loginOrLogout() {
    var a = document.querySelector("#login-logout");
    jQuery.ajax({
        type: "GET",
        url: "/user/login-logout",
        date: {},
        success: function (result) {
            if (result != null && result.code == 200) {
                //这里有两种情况：登录 / 未登录
                if (result.data == 1) {
                    //登录——注销按钮
                    a.href = "javascript:logout()";
                    a.innerHTML = "注销";
                } else {
                    //未登录——登录按钮
                    a.href = "/login.html";
                    a.innerHTML = "登录"
                }
            }
        }
    });
}


//我的主页(跳转)
function myIndex() {
    jQuery.ajax({
        type: "POST",
        url: "/user/myid",
        data: {},
        success: function(result) {
            if(result != null && result.code == 200) {
                location.assign("/myblog_list.html?id="+result.data);
            } else {
                location.assign("/login.html");
            }
        }
    });
}

//博客草稿页(跳转)
function mydraft() {
    jQuery.ajax({
        type: "POST",
        url: "/user/myid",
        data: {},
        success: function(result) {
            if(result != null && result.code == 200) {
                location.assign("/draft_list.html?id="+result.data);
            } else {
                location.assign("login.html");
            }
        }
    });
}




//定时发布
function myTimeSub() {
    var postTime = jQuery("#pubdate");
    //非空校验
    if (postTime.val() == "") {
        alert("请选择定时发布时间!");
        return;
    }
    if(confirm("是否确认发布!")) {
        jQuery.ajax({
            type: "POST",
            url: "/art/timeadd",
            data: {
                "title": jQuery('#title').val(),
                "content": editor.getValue(),
                "postTime": postTime.val()
            },
            success: function (result) {
                //result.data 是当前用户的 id
                if (result != null && result.code == 200) {
                    alert("定时发布成功!");                       
                    location.assign("/myblog_list.html?id=" + result.data);
                } else {
                    alert("定时发布失败，请稍后重试");
                }
            }
        });
    }
}


//点击关注按钮触发
//followId：要操作的 id
//flag：当前是否关注
//source：表示来源
function clickFollow(followId, flag, source) {
    if(!flag) {
        //关注
        jQuery.ajax({
            type: "POST",
            url: "/follow/select",
            data: {
                "followId": followId
            },
            success: function(result) {
                if(result != null && result.code == 200 && result.data == 1) {
                    if(source == null || source == "") {
                        location.href = location.href;
                    } else if(source == 1) { //否则就是在个人主页进行关注，刷新个人粉丝列表即可
                        getFanList();                       
                    } else if(source == 2) {
                        getFollowList();
                    }
                } else {
                    alert(result.msg);
                }
            }
        });
    } else {
        //取消关注
        jQuery.ajax({
            type: "POST",
            url: "/follow/cancel",
            data: {
                "followId": followId
            },
            success: function(result) {
                if(result != null && result.code == 200 && result.data == 1) {
                    //若被关注的人就是作者，那么刷新个人信息即可
                    if(source == null || source == "") {
                        location.href = location.href;
                    } else if(source == 1) { //否则就是在个人主页进行关注，刷新个人粉丝列表即可
                        getFanList();                       
                    } else if(source == 2) {
                        getFollowList();
                    }
                } else {
                    alert(result.msg);
                }
            }
        });
    }
}